home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 526-550 / disk_540 / browser / browserii_src.lzh / ActionBack.c next >
C/C++ Source or Header  |  1991-07-12  |  10KB  |  340 lines

  1. /*
  2.  *    ActionBack.c - Copyright © 1991 by S.R. & P.C.
  3.  *
  4.  *    Created:    21 Apr 1991  16:09:52
  5.  *    Modified:    12 Jul 1991  20:43:07
  6.  *
  7.  *    Make>> make
  8.  */
  9.  
  10.  
  11. #include "Global.h"
  12. #include "FileList.h"
  13. #include "ActionBack.h"
  14. #include "Process.h"
  15. #include "proto/ActionBack.h"
  16. #include "proto/Process.h"
  17. #include "proto/Windows.h"
  18. #include "proto/Mouse.h"
  19. #include "proto/Scan.h"
  20. #include "proto/Sort.h"
  21. #include "proto/Draw.h"
  22. #include "proto/String.h"
  23. #include "proto/File.h"
  24. #include "proto/FileList.h"
  25.  
  26. extern struct ExecBase *SysBase;
  27. extern struct MinList WindowList;
  28. extern struct Config Config;
  29. extern struct MsgPort *ProcessPort;
  30.  
  31.  
  32. #define AB_DELETE        (AB_DELETE_DEST|AB_DELETE_SOURCE)
  33.  
  34.  
  35. /* Search by name a ScrollEntry in a window */
  36.  
  37. static struct ScrollEntry *FindEntry(struct BrowserWindow *Win, char *Name)
  38. {
  39.     struct ScrollEntry *S;
  40.     short i;
  41.  
  42.     /* No list for main window, only an array */
  43.     if (Win->bw_Type == BW_MAIN) {
  44.         for( i=0 ; i < Win->bw_ShownEntries ; i++ ) {
  45.             S = Win->bw_EntryArray[i];
  46.             if (!Strcmp(S->se_FileInfo.fi_Name, Name))
  47.                 return (S->se_State & STATE_DELETED) ? NULL : S;
  48.         }            
  49.     }
  50.     else {
  51.         S = (struct ScrollEntry *)Win->bw_EntryList.mlh_Head;
  52.         while(S->se_Node.mln_Succ) {
  53.             if (!Strcmp(S->se_FileInfo.fi_Name, Name))
  54.                 return (S->se_State & STATE_DELETED) ? NULL : S;
  55.             S = (struct ScrollEntry *)S->se_Node.mln_Succ;
  56.         }
  57.     }
  58.     return NULL;
  59. }
  60.  
  61.  
  62. /* Search by name a ScrollEntry in NewEntryList of a window */
  63.  
  64. static struct ScrollEntry *FindNewEntry(struct BrowserWindow *Win, char *Name)
  65. {
  66.     struct ScrollEntry *S;
  67.  
  68.     S = (struct ScrollEntry *)Win->bw_NewEntryList.mlh_Head;
  69.     while(S->se_Node.mln_Succ) {
  70.         if (!Strcmp(S->se_FileInfo.fi_Name, Name))
  71.             return S;
  72.         S = (struct ScrollEntry *)S->se_Node.mln_Succ;
  73.     }
  74.     return NULL;
  75. }
  76.  
  77.  
  78. static void ActionBack(struct SuperFileInfo *sfi, BPTR DirLock, short Action)
  79. {
  80.     struct BrowserWindow *Win;
  81.     struct ScrollEntry *S = NULL;
  82.     struct Point Position;
  83.     BOOL PrintString = FALSE;
  84.     short len;
  85.  
  86.     if (!Action)
  87.         return;
  88.     if (Action & AB_SCANDEVS) {
  89.         ScanDevs();
  90.         return;
  91.     }
  92.     switch(sfi->FileInfo.fi_Type) {
  93.     case DLX_FILE:
  94.     case DLX_DIR:
  95.         Win = FindDir(DirLock);
  96.         break;
  97.     default:    /* device, volume, assign */
  98.         Win = (struct BrowserWindow *)WindowList.mlh_Head;
  99.     }
  100.     if (Win) {
  101.         if (Action & AB_NEW_ENTRY) {
  102.             if (S = FindEntry(Win, sfi->FileInfo.fi_Name)) {
  103.                 /* size of new entry may be different, so... */
  104.                 Win->bw_NumBytes -= S->se_FileInfo.fi_Size;
  105.                 Win->bw_NumBlocks -= S->se_FileInfo.fi_NumBlocks;
  106.                 Win->bw_NumBytes += sfi->FileInfo.fi_Size;
  107.                 Win->bw_NumBlocks += sfi->FileInfo.fi_NumBlocks;
  108.                 if (S->se_Print) {
  109.                     Win->bw_ShownBytes -= S->se_FileInfo.fi_Size;
  110.                     Win->bw_ShownBlocks -= S->se_FileInfo.fi_NumBlocks;
  111.                     Win->bw_ShownBytes += sfi->FileInfo.fi_Size;
  112.                     Win->bw_ShownBlocks += sfi->FileInfo.fi_NumBlocks;
  113.                     CopyFileInfo(&S->se_FileInfo, &sfi->FileInfo);
  114.                     if ((len = strlen(S->se_FileInfo.fi_Name)) > Win->bw_MaxFilenameLen) {
  115.                         Win->bw_Flags |= BWF_REBUILD_STRINGS;
  116.                         Win->bw_NewMaxFilenameLen = MAX(len, Win->bw_NewMaxFilenameLen);
  117.                     }
  118.                     else {
  119.                         BuildPrintString(Win, S);
  120.                         PrintString = TRUE;
  121.                     }
  122.                 }
  123.                 else
  124.                     CopyFileInfo(&S->se_FileInfo, &sfi->FileInfo);
  125.                 MakeBottomInfoString(Win);
  126.                 RefreshBottomInfo(Win);
  127.             }
  128.             else if (S = FindNewEntry(Win, sfi->FileInfo.fi_Name)) {
  129.                 CopyFileInfo(&S->se_FileInfo, &sfi->FileInfo);
  130.             }
  131.             else if (S = AllocMem(sizeof(struct ScrollEntry), MEMF_PUBLIC|MEMF_CLEAR)) {
  132.                 CopyFileInfo(&S->se_FileInfo, &sfi->FileInfo);
  133.                 /* entry can only be a file or dir here, so set pen to 1 or 3 */
  134.                 S->se_Pen = (S->se_FileInfo.fi_Type == DLX_FILE) ? 1 : 3;
  135.                 AddTail((struct List *)&Win->bw_NewEntryList, (struct Node *)S);
  136.                 Win->bw_NumNewEntries++;
  137.                 Win->bw_Flags |= BWF_REBUILD_ARRAY;
  138.                 if ((len = strlen(S->se_FileInfo.fi_Name)) > Win->bw_MaxFilenameLen) {
  139.                     Win->bw_Flags |= BWF_REBUILD_STRINGS;
  140.                     Win->bw_NewMaxFilenameLen = MAX(len, Win->bw_NewMaxFilenameLen);
  141.                 }
  142.             }
  143.         }
  144.         else if (S = FindEntry(Win, sfi->OldName)) {
  145.             if (Action & AB_DELETE) {
  146.                 if (S->se_State & STATE_SELECTED)
  147.                     DoSelect(Win, S, OPT_TOGGLESELECT);    /* deselect entry */
  148.                 S->se_State = STATE_DELETED;
  149.                 Win->bw_NumDeleted++;
  150.                 Win->bw_Flags |= BWF_REBUILD_ARRAY;
  151.                 /* if this file had the longest filename, tell DoUpdateDir() to check if
  152.                  * strings could be made smaller */
  153.                 if (strlen(sfi->OldName) == Win->bw_MaxFilenameLen)
  154.                     Win->bw_Flags |= BWF_CHECK_MAXLEN;
  155.                 PrintString = TRUE;
  156.             }
  157.             if (S->se_State & STATE_GHOSTED) {    /* change state only if ghosted */
  158.                 S->se_State &= ~STATE_GHOSTED;
  159.                 if (S->se_Print) {
  160.                     if (Action & AB_SELECT)
  161.                         DoSelect(Win, S, 0);
  162.                     PrintString = TRUE;
  163.                 }
  164.             }
  165.             if (S->se_Print) {
  166.                 if (Action & AB_UPDATE_STRING) {
  167.                     CopyFileInfo(&S->se_FileInfo, &sfi->FileInfo);
  168.                     BuildPrintString(Win, S);
  169.                     PrintString = TRUE;
  170.                 }
  171.                 if (Action & AB_RENAME_ENTRY) {
  172.                     CopyFileInfo(&S->se_FileInfo, &sfi->FileInfo);
  173.                     Win->bw_Flags |= BWF_SORT;
  174.                     if ((len = strlen(S->se_FileInfo.fi_Name)) > Win->bw_MaxFilenameLen) {
  175.                         Win->bw_Flags |= BWF_REBUILD_STRINGS;
  176.                         Win->bw_NewMaxFilenameLen = MAX(len, Win->bw_NewMaxFilenameLen);
  177.                     }
  178.                     else {
  179.                         /* if this file had the longest filename, tell DoUpdateDir() to check if
  180.                          * strings could be made smaller */
  181.                         if (strlen(sfi->OldName) == Win->bw_MaxFilenameLen && len <Win->bw_MaxFilenameLen)
  182.                             Win->bw_Flags |= BWF_CHECK_MAXLEN;
  183.                         BuildPrintString(Win, S);
  184.                         PrintString = TRUE;
  185.                     }
  186.                 }
  187.                 /* Modified entry may no more pass through filters so check it */
  188.                 if (!MatchFilters(&S->se_FileInfo, &Win->bw_FiltersInfo))
  189.                     Win->bw_Flags |= BWF_REBUILD_ARRAY|BWF_REBUILD_STRINGS;
  190.             }
  191.         }
  192.         else if (S = FindNewEntry(Win, sfi->OldName)) {
  193.             if (Action & AB_DELETE) {
  194.                 Remove((struct Node *)S);
  195.                 Win->bw_NumNewEntries--;
  196.             }
  197.             else
  198.                 CopyFileInfo(&S->se_FileInfo, &sfi->FileInfo);
  199.         }
  200.         if (PrintString && GetSePosition(Win, S, &Position))
  201.             Print(Win, S, Position.X, Position.Y);
  202.     }
  203. }
  204.  
  205.  
  206. void DoActionBack(struct SuperFileInfo *sfi, BPTR SrcDir, BPTR DestDir)
  207. {
  208.     if (sfi->ActionBack == AB_NONE)
  209.         return;
  210.     if (DestDir)
  211.         ActionBack(sfi, DestDir, sfi->ActionBack & AB_DEST_FLAGS);
  212.     /* Don't check SrcDir, it may be the main window */
  213.     ActionBack(sfi, SrcDir, sfi->ActionBack & AB_SOURCE_FLAGS);
  214. }
  215.  
  216.  
  217. void SendActionBack(struct SuperFileInfo *sfi, BPTR SrcDir, BPTR DestDir)
  218. {
  219.     struct TaskData *TaskData;
  220.     struct BrowserMsg *Msg;
  221.  
  222.     if (sfi->ActionBack == AB_NONE)
  223.         return;
  224.     if (Config.Options & OPT_ASYNCHRONOUS) {
  225.         TaskData = (struct TaskData *)SysBase->ThisTask->tc_UserData;
  226.         FreeProcessRepliedMsg(TaskData);
  227.         if (!(Msg = AllocBrowserMsg()))
  228.             return;
  229.         Msg->bm_ExecMessage.mn_ReplyPort = TaskData->td_ReplyPort;
  230.         Msg->bm_Type = BM_ACTIONBACK;
  231.         Msg->bm_Info.ActionBack.SrcDir = SrcDir;
  232.         Msg->bm_Info.ActionBack.DestDir = DestDir;
  233.         /* Copy SuperFileInfo */
  234.         Msg->bm_Info.ActionBack.sfi = *sfi;
  235.         if (sfi->FileInfo.fi_Comment)
  236.             Msg->bm_Info.ActionBack.sfi.FileInfo.fi_Comment = CopyStr(sfi->FileInfo.fi_Comment);
  237.         PutMsg(ProcessPort, (struct Message *)Msg);
  238.         TaskData->td_MsgCount++;
  239.     }
  240.     else
  241.         DoActionBack(sfi, SrcDir, DestDir);
  242.     sfi->ActionBack = AB_NONE;
  243. }
  244.  
  245.  
  246. void DoUpdateDir(BPTR Dir)
  247. {
  248.     struct BrowserWindow *Win;
  249.     struct ScrollEntry *S, *Next;
  250.     short len=0, i;
  251.     BOOL Ok = TRUE;
  252.     UBYTE Flags;
  253.  
  254.     if (Win = FindDir(Dir)) {
  255.         if (Flags = Win->bw_Flags) {
  256.             /* if strings have to be rebuilt, they must be longer, so don't check if
  257.              * they can be made smaller !! */
  258.             if ((Flags & BWF_CHECK_MAXLEN) && !(Flags & BWF_REBUILD_STRINGS)) {
  259.                 for( i=0 ; i<Win->bw_ShownEntries ; i++ ) {
  260.                     S = Win->bw_EntryArray[i];
  261.                     if (!(S->se_State & STATE_DELETED))
  262.                         len = MAX(len, strlen(S->se_FileInfo.fi_Name));
  263.                 }
  264.                 if (len < Win->bw_MaxFilenameLen) {
  265.                     Win->bw_MaxFilenameLen = len;
  266.                     Flags |= BWF_REBUILD_STRINGS;
  267.                 }
  268.             }
  269.             if ((Flags & BWF_SORT) && !(Flags & BWF_REBUILD_ARRAY) && (Win->bw_Sort & 0x0F) == NAME_SORT)
  270.                 Sort(Win);
  271.             if ((Flags & BWF_REBUILD_STRINGS) && !(Flags & BWF_REBUILD_ARRAY)) {
  272.                 Ok = BuildPrintStrings(Win);
  273.             }
  274.             else if ((Flags & BWF_REBUILD_ARRAY) && !(Flags & BWF_REBUILD_STRINGS)) {
  275.                 if (Ok = UpdateEntryList(Win, TRUE)) {
  276.                     Win->bw_ShownFiles = Win->bw_ShownDirs = Win->bw_ShownEntries = 0;
  277.                     Win->bw_ShownBytes = Win->bw_ShownBlocks = 0;
  278.                     S = (struct ScrollEntry *)Win->bw_EntryList.mlh_Head;
  279.                     i = 0;
  280.                     while(Next = (struct ScrollEntry *)S->se_Node.mln_Succ) {
  281.                         if (S->se_Print) {
  282.                             Win->bw_EntryArray[i++] = S;
  283.                             if (S->se_FileInfo.fi_Type == DLX_FILE)
  284.                                 Win->bw_ShownFiles++;
  285.                             else
  286.                                 Win->bw_ShownDirs++;
  287.                             Win->bw_ShownBytes += S->se_FileInfo.fi_Size;
  288.                             Win->bw_ShownBlocks += S->se_FileInfo.fi_NumBlocks;
  289.                         }
  290.                         S = Next;
  291.                     }
  292.                     Win->bw_ShownEntries = i;
  293.                     Sort(Win);
  294.                     MakeBottomInfoString(Win);
  295.                 }
  296.             }
  297.             else {    /* both flags BWF_REBUILD_ARRAY and BWF_REBUILD_STRINGS are set */
  298.                 if (Ok = List2Array(Win))
  299.                     MakeBottomInfoString(Win);
  300.             }
  301.             Win->bw_Flags = 0;
  302.             if (Ok)
  303.                 RefreshWindow(Win);
  304.             else
  305.                 CloseBrowserWindow(Win);
  306.         }
  307.         else {    /* some entries may have been reselected, so update bottom info */
  308.             MakeBottomInfoString(Win);
  309.             RefreshBottomInfo(Win);
  310.         }
  311.     }
  312. }
  313.  
  314.  
  315. void SendUpdateDir(BPTR Dir)
  316. {
  317.     struct TaskData *TaskData;
  318.     struct BrowserMsg *Msg;
  319.  
  320.     if (!Dir)
  321.         return;
  322.     if (Config.Options & OPT_ASYNCHRONOUS) {
  323.         TaskData = (struct TaskData *)SysBase->ThisTask->tc_UserData;
  324.         if (!(Msg = AllocBrowserMsg()))
  325.             return;
  326.         Msg->bm_ExecMessage.mn_ReplyPort = TaskData->td_ReplyPort;
  327.         Msg->bm_Type = BM_UPDATEDIR;
  328.         Msg->bm_Info.Dir = Dir;
  329.         PutMsg(ProcessPort, (struct Message *)Msg);
  330.         TaskData->td_MsgCount++;
  331.         /* Wait for all pending messages before unlocking Dir, or locks given to
  332.          * actionback messages won't be valid anymore */
  333.         WaitAllMsg(TaskData);
  334.     }
  335.     else
  336.         DoUpdateDir(Dir);
  337. }
  338.  
  339.  
  340.